﻿using System;
using FluentIoC;
using QuickStart.Components;
using QuickStart.Configuration;
using QuickStart.Services;

namespace QuickStart
{
    class Program
    {
        static void Main()
        {
            //If you have configured all of your services in xml, you can access that configuration without 
            //any extra work
            var configService = IoC.Resolve<IConfigService>();

            //Load the IoC configuration class 
            IoC.Configure(typeof (IoCConfiguration));

            //The ioc is now configured & you can resolve services as normal
            var bar = IoC.Resolve<IBarService>();
            var foo = IoC.Resolve<IFooService>();
            var factory = IoC.Resolve<IFactoryService>();
            var generic = IoC.Resolve<IGenericService<string>>();

            //You can set the properties of components easily
            //e.g. .SetProperty<BarService>(c => c.Test = "Hello world")
            var test = (bar as BarService).Test;

            //You can also set the values of properties to use other services/components 
            //e.g. .SetProperty<FooService>(c => c.Bar)
            bar = (foo as FooService).Bar;

            //For more complicated senarios, factories can be used
            //e.g. here, i have used the factory FactoryFactory to inject a message 
            var message = (factory as FactoryService).Message;
        }

        /// <summary>
        /// Examples of other ways to configure IoC
        /// </summary>
        private void AlternativeConfigurationMethods()
        {
            //This will look for instances of ContainerConfiguration in the current assembly
            IoC.ConfigureFromAssembly();

            //Or you can specify the assembly which contains the instances of ContainerConfiguration
            IoC.ConfigureFromAssembly("QuickStart.Configuration");

            //Alternativly, if you dont want to create a seperate class you can configure IoC inline
            IoC.Configure(
                
                c => c.Register<IBarService>()
                      .ImplementedBy<BarService>()
                      .SetProperty<BarService>(p => p.Test = "Lamda's r0ck!"),

                c => c.RegisterAssembly("QuickStart.Services")
                      .ImplementedBy("QuickStart.Components")
                
            );
        }
    }
}
